/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-09-04 17:12:21
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-09-12 17:46:53
 * @FilePath: /react18-vite-admin/src/utils/storage.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
    ACCESS_TOKEN,
    THEME_MODE,
    THEME_PRIMARY,
    APP_SETTINGS,
    SIDEBAR_STATUS,
    MY_INFO
} from '@/constants/storage';

export const getAccessToken = (): string | undefined => {
    return window.localStorage.getItem(ACCESS_TOKEN) ?? undefined;
};

export const setAccessToken = (token: string) => {
    return window.localStorage.setItem(ACCESS_TOKEN, token);
};

export const removeAccessToken = () => {
    return window.localStorage.removeItem(ACCESS_TOKEN);
};
export const getThemeMode = (): 'default' | 'dark' | undefined => {
    return (
        (window.localStorage.getItem(THEME_MODE) as
            | 'default'
            | 'dark'
            | null) || undefined
    );
};

export const setThemeMode = (mode: 'default' | 'dark') => {
    return window.localStorage.setItem(THEME_MODE, mode);
};

export const clearThemeMode = () => {
    return window.localStorage.removeItem(THEME_MODE);
};
export const getThemePrimary = (): string | undefined => {
    return window.localStorage.getItem(THEME_PRIMARY) ?? undefined;
};
export const setThemePrimary = (value: string) => {
    return window.localStorage.setItem(THEME_PRIMARY, value);
};

export const clearThemePrimary = () => {
    return window.localStorage.removeItem(THEME_PRIMARY);
};

export const getAppSettings = (): AppSettings | undefined => {
    const settings = window.localStorage.getItem(APP_SETTINGS);
    return settings !== null ? JSON.parse(settings) : undefined;
};

export const setAppSettings = (value: AppSettings) => {
    return window.localStorage.setItem(APP_SETTINGS, JSON.stringify(value));
};

export const clearAppSettings = () => {
    return window.localStorage.removeItem(APP_SETTINGS);
};

export const getSidebarStatus = (): AppSidebarStatus | undefined => {
    return (
        (window.localStorage.getItem(
            SIDEBAR_STATUS
        ) as AppSidebarStatus | null) ?? undefined
    );
};

export const setSidebarStatus = (value: AppSidebarStatus) => {
    return window.localStorage.setItem(SIDEBAR_STATUS, value);
};

export const clearSidebarStatus = () => {
    return window.localStorage.removeItem(SIDEBAR_STATUS);
};

export const getMyInfo = (): MyInfo | undefined => {
    const myInfo = window.localStorage.getItem(MY_INFO);
    return myInfo !== null ? JSON.parse(myInfo) : undefined;
};

export const setMyInfo = (value: MyInfo) => {
    return window.localStorage.setItem(MY_INFO, JSON.stringify(value));
};

export const clearMyInfo = () => {
    return window.localStorage.removeItem(MY_INFO);
};
